#include <stdint.h>

#include "vicl.h"

int install_guest_vm_tools(int idx)
{
    VixToolsState powerState;

    if(idx >= MAX_VMS)
        return -1;
        
    if(vmachine[idx].usable == false)
        return -1;
    
    VICL_STATUS("Querying the VM's power state");
    if((powerState = query_virtual_machine_power_state(idx)) == -1)
        return -1;
    if(powerState & (VIX_POWERSTATE_POWERED_OFF | VIX_POWERSTATE_POWERING_OFF | VIX_POWERSTATE_SUSPENDED))
    {
        VICL_ERROR("The VM is already powered/powering off");
        return -1;
    }
    
    jobHandle = VixVM_InstallTools(vmachine[idx].handle,
                                   0,       // options
                                   NULL,    // commandLineArgs
                                   NULL,    // callbackProc
                                   NULL);   // clientData

    err = VixJob_Wait(jobHandle,
                      VIX_PROPERTY_NONE);
    
    if(VIX_OK != err)
    {
        VICL_ERROR("Install VMtools: failed, error 0x%x", err);
        return -1;
    }
    
    Vix_ReleaseHandle(jobHandle);
    
    return 0;
}

